package com.shrio.word.poi;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.Rows;
import com.deepoove.poi.util.PoitlIOUtils;

import java.io.*;
import java.util.*;

public class TestExport {

    public static void main(String[] args) throws Exception{
        // 获取模板文件流
        InputStream resourceAsStream = new FileInputStream(new File("E:/word/poi-tl.docx"));
        //poi-tl 配置
        ConfigureBuilder builder = Configure.builder();
        builder.useSpringEL(false);

        Map<String,Object> map = new HashMap<String,Object>();

        // 伪造一个表格数据
        //单个表格
        ServerTableData oneTable = getServerTableData();
        map.put("oneTable",oneTable);
        builder.bind("oneTable",new ServerTablePolicy());

        XWPFTemplate template = XWPFTemplate.compile(Objects.requireNonNull(resourceAsStream), builder.build()).render(map);
        // HttpServletResponse response
        OutputStream out = new FileOutputStream(new File("E:/word/poi-tl2.docx"));
        BufferedOutputStream bos = new BufferedOutputStream(out);
        template.write(bos);
        bos.flush();
        out.flush();
        PoitlIOUtils.closeQuietlyMulti(template, bos, out);

    }

    private static ServerTableData getServerTableData() {
        ServerTableData serverTableData = new ServerTableData();
        List<RowRenderData> serverDataList = new ArrayList<RowRenderData>();
        for (int j = 0; j < 4; j++) {
            String typeName;
            RowRenderData serverData;
            if (j > 1) {
                typeName = "索隆";
                serverData = Rows.of(typeName, "喝酒", "三千世界", "无").center().create();
            }else {
                typeName = "路飞";
                serverData = Rows.of(typeName, "大鸡腿", "巨人手枪", "橡胶果实").center().create();
            }
            serverDataList.add(serverData);
        }

        List<Map<String, Object>> groupDataList = new ArrayList<Map<String, Object>>();
        Map<String, Object> groupData1 = new HashMap<String, Object>();
        groupData1.put("typeName", "索隆");
        groupData1.put("listSize", "2");
        Map<String, Object> groupData2 = new HashMap<String, Object>();
        groupData2.put("typeName", "路飞");
        groupData2.put("listSize", "2");
        groupDataList.add(groupData1);
        groupDataList.add(groupData2);

        serverTableData.setServerDataList(serverDataList);
        serverTableData.setGroupDataList(groupDataList);
        serverTableData.setMergeColumn(0);
        return serverTableData;
    }

}
